angular.module('meRecommendListcontrollers', [])

  .controller('MeRecommendListCtrl', function ($scope, $ionicActionSheet, $state, $timeout, $ionicPopup, Storage, History,MeRecommendList, MeRecommendOrderList,MeHistoryOrderDetail,Toast,Loading, ENV,MeHistoryList) {

    var memberId = Storage.get("member").member_id;
    $scope.ENV = ENV;
    $scope.selectWineCount = 0;


    //进入界面时，加载数据
    var fistInRecommend = true;
    $scope.$on('$ionicView.beforeEnter', function () {
      if(fistInRecommend){
        Loading.showLoading();
        fistInRecommend = false;
      }
      MeRecommendList.getRecommendListInfo(memberId);
      $scope.addWineStatus = MeHistoryOrderDetail.getAddMoreWineStatus();
    });

    // 获取后台返回的数据
    $scope.$on('Me.getRecommendListInfo', function () {
      Loading.hideLoading();
      var result = MeRecommendList.getRecommendListJson();
      $scope.wineList = result.wineList;
    });

    $scope.changeSelectState = function (index) {
      if($scope.wineList[index].selected){
        $scope.selectAllWine = false;
        $scope.selectWineCount--;
      }else{
        $scope.selectWineCount++;
        if($scope.selectWineCount==$scope.wineList.length){
          $scope.selectAllWine = true;
        }
      }
      $scope.wineList[index].selected = !$scope.wineList[index].selected;
    };

    //点击全选
    $scope.selectAllWines = function () {
      $scope.selectAllWine = !$scope.selectAllWine;
      if ($scope.selectAllWine) {
        $scope.selectWineCount = $scope.wineList.length;
        //将所有酒款都选中
        for (var i = 0; i < $scope.wineList.length; i++) {
          $scope.wineList[i].selected = true;
        }
      } else {
        $scope.selectWineCount = 0;
        //将所有酒款都去掉选中
        for (var j = 0; j < $scope.wineList.length; j++) {
          $scope.wineList[j].selected = false;
        }
      }
    };
    $scope.$on('$ionicView.beforeLeave',function(){
      $scope.selectWineCount = 0;
    });
    //生成推荐酒单，跳转到推荐酒单编辑界面
    $scope.createRecommendWine = function () {
      //获取选中的酒款
      var getWineList = $scope.wineList;
      var selectWineList = [];
      for (var i = 0; i < getWineList.length; i++) {
        if (getWineList[i].selected) {
          selectWineList.push(getWineList[i]);
        }
      }
      if(selectWineList.length == 0){
        Toast.showMessage("请选择要生成推荐酒单的的酒款");
      }else{
        if($scope.addWineStatus){
          //为历史酒单添加酒款
          MeRecommendList.setSelectWineForAdd(selectWineList);
          $state.go('history-order-detail');
          $scope.selectAllWine = false;
          MeHistoryOrderDetail.setAddMoreWineStatus(false);
        }else{
          //生成新的推荐酒单
          MeRecommendOrderList.setSelectWineList(selectWineList);
          $state.go('recommend-order-list');
          MeHistoryList.setGo2CreateRe(true);
          $scope.selectAllWine = false;
        }

      }
    };

    //删除选中的酒款
    $scope.deleteRecommendWine = function () {
      var getWineList = $scope.wineList;
      var deleteWineIdList = [];
      for (var i = 0; i < getWineList.length; i++) {
        if (getWineList[i].selected) {
          deleteWineIdList.push(getWineList[i].mnw_id);
        }
      }
      if (deleteWineIdList.length == 0) {
        Toast.showMessage("请选择要删除的酒款");
      } else {
        // 弹出确定窗口
        var confirmPopup = $ionicPopup.confirm({
          template: '<p style="text-align: center;font-size:16px">确定要删除这些酒款么？</p>',
          cancelText: '取消',
          okText: '确定',
          okType: 'hd-red-btn',
          cancelType: 'cancel-esc-btn'
        });
        confirmPopup.then(function (res) {
          if (res) {
            //删除对应的酒款
            Loading.showLoading();
            MeRecommendList.deleteWine(memberId,deleteWineIdList);
          }
        });
      }
    };

    $scope.$on('Me.deleteWine', function () {
      Loading.hideLoading();
      var result = MeRecommendList.getDeleteWineJson();
      if(result.recommendStatus){
        //刷新界面
        Toast.showMessage(result.recommendMsg);
        $scope.wineList = result.wineList;
        $scope.selectAllWine = false;
        $scope.selectWineCount = 0;
      }
    });

    //改变数字的显示，添加逗号分割
    $scope.getFormatNum = function (num) {
      return formatNum(num);
    };

	 //返回
    $scope.goback = function(){
      $state.go('tab.meIndex');
    }

    $scope.go2HistoryList = function(){
      $state.go("history-list");
      MeHistoryList.setGo2CreateRe(false);
    }

  });
